package com.yunshang.ys.ys;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.widget.Toast;


import com.yunshang.ys.api.DownImageApi;
import com.yunshang.ys.utils.ComplexUtil;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/2/22.
 */

public class ScanCodeImageUtil {

    private Context mContext;
    public  Bitmap mBitmap;


    public ScanCodeImageUtil(Context mContext) {
        this.mContext = mContext;
    }

    public void netDownImage(String url, final String id, final ImageView iv) {
        YSHttp.createApi(DownImageApi.class)
//                .downImage("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1487735318915&di=72259538d1f32ab57ab2f72db0524f80&imgtype=0&src=http%3A%2F%2Ffile25.mafengwo.net%2FM00%2F0A%2FAA%2FwKgB4lMC256AYLqGAAGklurKzyM52.rbook_comment.w1024.jpeg")
                .downImage(url)
                .subscribeOn(Schedulers.newThread())
                .map(new Func1<ResponseBody, Bitmap>() {
                    @Override
                    public Bitmap call(ResponseBody responseBody) {
                        byte[] bytes = new byte[0];
                        try {
                            bytes = responseBody.bytes();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        if (bitmap != null)
                            return bitmap;
                        return null;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Bitmap>() {
                    ProgressDialog dialog = new ProgressDialog(mContext);

                    @Override
                    public void onStart() {
                        dialog.show();
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {
                        dialog.dismiss();
                        System.out.println("completed");
                    }

                    @Override
                    public void onError(Throwable e) {
                        dialog.dismiss();
                        System.out.println("onerror");
                        Toast.makeText(mContext, "image error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                        mBitmap = bitmap;
                        System.out.println("onnext");
                        dialog.dismiss();
//                        mBitmap = CodeUtils.createImage(id, 500, 500, ComplexUtil.getRoundedCornerBitmap(ComplexUtil.ImageCrop(bitmap, false), 10));
//                        iv.setImageBitmap(mBitmap);
                        iv.setImageBitmap(bitmap);
                    }
                });
    }

    public Bitmap getmBitmap() {
        if (mBitmap != null)
            return mBitmap;
        else return null;
    }
}
